<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<!DOCTYPE html>
<html>
<head>
    <title>专家抽取规则</title>
    <%@ include file="/WEB-INF/component/commonCSS.jsp"%>
    <!--[if lt IE 9]>
        <script src="${ctx}/lib/js/html5shiv.min.js"></script>
        <script src="${ctx}/lib/js/respond.min.js"></script>
    <![endif]-->
</head>
<body class="skin-blue-light sidebar-mini fixed skin-blue-light-frame">
							
    <section class="content-header">
        <h1>专家抽取规则</h1>
        <ol class="breadcrumb">
            <li><a href="${ctx}/home.jsp"><i class="fa fa-home"></i>首页</a></li>
            <li class="active"><a href="#">专家抽取管理</a></li>
            <li class="active"><a href="#">抽取规则设置</a></li>
        </ol>
    </section>
    
    <section class="content">
      <form id="form" method="post" class="form-horizontal">
        <div class=" box box-success">
		<div class="box-body"  >
		    <div class="input">
                    <div class="textalign1">所属项目：</div>
                    <div class="boxinput">
                        <select class='textinput' id="projectId">
							<option value="">请选择</option>
							<c:forEach items="${projectList}" var="project">
								<option value="${project.projId }" >${project.name}</option>
							</c:forEach>
						</select>
                    </div>
                    <div class="textalign">抽取状态：</div>
                    <div class="boxinput">
                        <select class='textinput' id="execStatus">
							<option value="">请选择</option>
							<option value="1">已抽取</option>
							<option value="0">未抽取</option>
						</select>
                    </div>
                    <div class="textalign">状态：</div>
                    <div class="boxinput">
                        <select class='textinput' id="status">
							<option value="">请选择</option>
							<option value="1">启用</option>
							<option value="0">停用</option>
						</select>
                    </div>
            </div>
            <div class="input" style="margin-top:-5px">
                    <div class="textalign1" >抽取日期：</div>
                    <div class="boxinput"> 
                        <table style="width:100%;">
                                <tr>
                                    <td style="width:50%">       
                                        <input class="textinput" type="text" id="execStartTime"  placeholder="yyyy-MM-dd" maxlength="10" onFocus="WdatePicker({maxDate:'#F{$dp.$D(\'execEndTime\')}'})"  />
						            </td>
                                    <td>&nbsp;-&nbsp;</td>
                                    <td style="width:50%">       
                                        <input class="textinput" type="text" id="execEndTime"  placeholder="yyyy-MM-dd" maxlength="10" onFocus="WdatePicker({minDate:'#F{$dp.$D(\'execStartTime\')}'})" />
                                    </td>
                                </tr>
                         </table>        
                    </div>
                    <div class="textalign">是否大输液：</div>
                    <div class="boxinput">
                        <select class='textinput' id="infusion">
							<option value="">请选择</option>
							<option value="1">是</option>
							<option value="0">否</option>
						</select>
                    </div>
                    <div class="textalign">
                        <button type="button" class="btn btn-success btn-sm" onclick="search();">查&nbsp;询</button>
                    </div>
            </div>
         </div>
        </div>            
    
       <!-- <div class=" box box-success">
            <div class="box-body">
                <div class="form-inline">
                    <div class="form-group form-group-xs" style="width:35%;">
						<label>所属项目：</label> 
						<select class='form-control' id="projectId"
							style="width: 250px;" disabled="disabled">
							<option value="">请选择</option>
							<c:forEach items="${projectList}" var="project">
								<option value="${project.projId }" >${project.name}</option>
							</c:forEach>
						</select>
					</div>
					
					<div class="form-group form-group-xs" style="width:28%;padding-left:12px;">
						<label>抽取状态：</label> 
						<select class='form-control' id="execStatus"
							style="width: 100px;">
							<option value="">请选择</option>
							<option value="1">已抽取</option>
							<option value="0">未抽取</option>
						</select>
					</div>
					<div class="form-group form-group-xs" style="width:28%;">
						<label>状态：</label> 
						<select class='form-control' id="status"
							style="width: 100px;">
							<option value="">请选择</option>
							<option value="1">启用</option>
							<option value="0">停用</option>
						</select>
					</div>
					 <div class="form-group form-group-xs" style="width:35%;">
                        <label for="searchPharCode">抽取日期：</label>
                        <input class="form-control" type="text" id="execStartTime"  placeholder="yyyy-MM-dd" maxlength="10" onFocus="WdatePicker({maxDate:'#F{$dp.$D(\'execEndTime\')}'})"  />
                        -
                        <input class="form-control" type="text" id="execEndTime"  placeholder="yyyy-MM-dd" maxlength="10" onFocus="WdatePicker({minDate:'#F{$dp.$D(\'execStartTime\')}'})" />
                    </div>
                    ${infusion==null?"":infusion?"":"<!--" }
					<div class="form-group form-group-xs" style="width:28%;">
						<label>是否大输液：</label> 
						<select class='form-control' id="infusion"
							style="width: 100px;">
							<option value="">请选择</option>
							<option value="1">是</option>
							<option value="0">否</option>
						</select>
					</div>
					${infusion==null?"":infusion?"":"-->" }
					
                    <button type="button" class="btn btn-success btn-sm" onclick="search();">查&nbsp;询</button>
                </div>
            </div>
        </div>-->
        </form>
        <table class="jqgrid" id="gridlist"></table>
        <div id="gridpage"></div>
        
        <div class=" box box-warning bottongroup">
             <a class="btn btn-default btn-sm" onclick="addExpertExtractRule()">新增规则</a>
             <label class="btn btn-default btn-sm" onclick="batchExtractExpert()">抽取专家</label>
              <c:if test="${defaultProjId==null }">
             	 <a href="toExpertExtractRuleList.html" class="btn btn-default btn-sm">返&nbsp;回</a>
              </c:if>
        </div>
        <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog" style="width:800px;height:700px;">
                <div class="modal-content" >
                    <div class="modal-header bg-primary">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h5 class="modal-title" id="exampleModalLabel">专家规则新增</h5>
                    </div>
                    <div class="modal-body">
                  <form id="formSubmit"  action="saveOrEditDrugQualitySpec.html">
					<div class="form-group form-group-xs">
						<label><span style="color:red">* </span>每家医疗机构最多可抽取专家数：</label> 
						<input id="maxExtractNum" value="1" name="maxExtractNum" oninput="value=value.replace(/\D/g,'')" />&nbsp;人
					</div>
					${infusion==null?"":infusion?"":"<!--" }
					<div class="form-group form-group-xs" onclick="checkedInfusion();">
						<input type="checkbox" id="checkedInfusion" />
						<label>大输液评审</label> 
					</div>
					${infusion==null?"":infusion?"":"-->" }
					<div class="form-group form-group-xs" style="display:none;">
						<label for="hospType" class="control-label"><span style="color:red">*</span>医疗机构分类：</label> <i class="fa fa-plus"  id="addHospType" style="color: blue;margin-right: 15px;cursor:pointer" onclick="addHospType()"></i>
						<div id="hospTypeDiv">
							<div style="height:24px"><select class="form-control" id="hospInfo"  name="hospInfo0" style="width: 190px;float:left" ></select><input class="form-control" id="hospInfoPer" name="hospInfoInput0" style="width: 50px;float:left;margin-left:30px" oninput="value=value.replace(/\D/g,'')"/><input id='ruleHospTypeId'  type='hidden' value=''/><div style="width:30px;float:left;margin-left:10px;">%</div></div>
					    </div>    
					</div>
					
					 <div class="form-group form-group-xs" style="float:left">
						<label for="expertType" class="control-label"><span style="color:red">*</span>选择专家类别：</label>  <i class="fa fa-plus"  id="addExpertType" style="color: blue;margin-right: 15px;cursor:pointer" onclick="addExpertType()"></i>
						<div id="expertTypeDiv">
							<div style="height:24px">
							<select	class="form-control" id="dicExpertType1"  style="width: 120px;float:left;" ></select>
							<select class="form-control" id="dicExpertType2"  style="width: 120px;float:left;" ></select>
						    <select class="form-control" id="dicExpertType3"  style="width: 120px;float:left" ><option value="">请选择</option></select>
						    <input class="form-control" id="dicExpertTypeNum" style="width: 50px;float:left;margin-left:30px" oninput="value=value.replace(/\D/g,'')"/>
						    <input id='bidExpertTypeId' type='hidden' value='' />
						    <div style="width:30px;float:left;margin-left:10px;">人</div>
						    </div>
					    </div>    
					</div>
					
                    <input type="hidden" id="ruleId" name="ruleId" value="${ruleId}"/>
                </form>
            		</div>
              <div class="modal-footer">
                  <button type="button" onclick="save();" class="btn btn-primary btn-sm">保&nbsp;存</button>
              </div>
            </div>
         </div>
      </div>
    </section>
    
      <%@ include file="/WEB-INF/component/commonJS.jsp"%>
    
    <script type="text/javascript">
    hFlag = "";//编辑的时候看医疗机构类型变多了还是少了
    eFlag = "";
    hCount = 0; //修改前医疗机构类型个数
    eCount = 0;
    ahCount = 0; //修改后医疗机构类型个数
    aeCount = 0; 
    
    //选择是否大输液
    function checkedInfusion(){
    	if($("#checkedInfusion").prop("checked")){
    		$("#hospTypeDiv").parent().show();
    	}else{
    		$("#hospTypeDiv").parent().hide();
    	}
    }
    
    //加医疗机构分类
    function addHospType(){
    	var maxNum = $("#hospInfo").children().length-1;
    	var num = $(".hospInfo").length+1;
    	if(num<maxNum){
    		$("#hospTypeDiv").append("<div style='width:350px;' class='del'><select class='form-control hospInfo' style='width: 190px;float:left' ></select><input class='form-control hospInfoPer' style='width: 50px;float:left;margin-left:30px' oninput=\"value=value.replace(/\\D/g,'')\"/><input class='ruleHospTypeIds' type='hidden' value=''/> <div style='width:30px;float:left;margin-left:10px;'>%</div><a href='javascript:void(0);' style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat -40px 0px' class='opIcon' onclick='toDelH(this);' title='删除' ></a></div>");
        	$("#hospTypeDiv").height($("#hospTypeDiv").height()+24);
        	loadHospType($($(".hospInfo")[$(".hospInfo").length-1]));
        	
    	}
    }
    
    //加专家类型
    function addExpertType(){
    	var maxExpertTypeCount = "${expertTypeCount}";
    	var expertTypeCount = $(".expertType1").length+1;
    	if(expertTypeCount<Number(maxExpertTypeCount)){
    		$("#expertTypeDiv").append("<div style='width:530px;' class='del'><select class='form-control expertType1'  style='width: 120px;float:left;' ></select><select class='form-control expertType2'  style='width: 120px;float:left;' ></select><select class='form-control expertType3'  style='width: 120px;float:left;' ><option value=''>请选择</option></select><input class='form-control expertTypeNum' style='width: 50px;float:left;margin-left:30px' oninput=\"value=value.replace(/\\D/g,'')\"/><input class='bidExpertTypeIds' type='hidden'  value=''/><div style='width:30px;float:left;margin-left:10px;'>人</div><a href='javascript:void(0);' style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat -40px 0px' class='opIcon' onclick='toDelE(this);' title='删除' ></a></div>");
        	getExperType($($(".expertType1")[$(".expertType1").length-1]),$($(".expertType2")[$(".expertType2").length-1]),$($(".expertType3")[$(".expertType3").length-1]));
    	}
    }
    
    //删除hosp
	function toDelH(obj){
    	$(obj).parents('.del').remove();
    	$("#hospTypeDiv").height($("#hospTypeDiv").height()-24);
    }
    
	 //删除expert
	function toDelE(obj){
    	$(obj).parents('.del').remove();
    }
	
    function loadHospType(obj){
    	obj.HNSelect({
            url: "${ctx}/selectController/getArchiveType.html", 
            data: { classifyCode: "113" },
            defaultText: "<option value=''>请选择</option>",
            defaultselect: "",
        });
    }
    
    function getExperType(obj1,obj2,obj3){//获取专家类别
    	obj1.HNSelect({
	        url: "${pageContext.request.contextPath}/selectController/getExpertType.html", data: { ID: '0' },  defaultText: "<option value=''>请选择</option>",
	        func: function () {
	        	obj2.HNSelect({
	                parent_selector: obj1, url: "${pageContext.request.contextPath}/selectController/getExpertType.html", dataid: "ID",defaultText: "<option value=''>请选择</option>",
	                		  func: function () {
	                			  obj3.HNSelect({
	                                  parent_selector: obj2, url: "${pageContext.request.contextPath}/selectController/getExpertType.html", dataid: "ID", defaultText: "<option value=''>请选择</option>"
	                              });
	                          }
	           		 });
	       		 }
	    	});
	  }
    
    
		 function getExperType1(preId){//获取专家类别
		    $("#"+preId+"1").HNSelect({
		        url: "${pageContext.request.contextPath}/selectController/getExpertType.html", data: { ID: '0' },  defaultText: "<option value=''>请选择</option>",
		        func: function () {
		            $("#"+preId+"2").HNSelect({
		                parent_selector: $("#"+preId+"1"), url: "${pageContext.request.contextPath}/selectController/getExpertType.html", dataid: "ID",defaultText: "<option value=''>请选择</option>",
		                		  func: function () {
		                              $("#"+preId+"3").HNSelect({
		                                  parent_selector: $("#"+preId+"2"), url: "${pageContext.request.contextPath}/selectController/getExpertType.html", dataid: "ID", defaultText: "<option value=''>请选择</option>"
		                              });
		                          }
		           		 });
		       		 }
		    	});
		  }
		 
	     function addExpertExtractRule(){//添加规则
    		//重置
			clear();
	     	$(".del").remove();
	     	$("#hospTypeDiv").height(24);
	     	
	     	loadHospType($("#hospInfo"));
    		getExperType1("dicExpertType");
    		$('#exampleModalLabel').html("新增抽取规则 - "+$("#projectId  option:checked").html());
			
    		$('#exampleModal').modal({
                keyboard: true
            });
    	}
	     
    	 function edit(ruleId){//编辑
    		clear();
 	     	$(".del").remove();
 	     	$("#hospTypeDiv").height(24);
 	     	
 	     	loadHospType($("#hospInfo"));
     		
     		
    		$('#exampleModalLabel').html("抽取规则编辑 - "+$("#projectId  option:checked").html()); 
    		$('#exampleModal').modal({
                keyboard: true
            }); 
    		$.post("${ctx}/expertExtractRule/toExpertExtractEdit.html", {
    				ruleId : ruleId
    		},		
    		function(result) {
    			if (result.success) {
    				$("#ruleId").val(ruleId);
    				$("#maxExtractNum").val(result.rows.maxExtractNum);
    				if($("#checkedInfusion")){
    	    			if(result.rows.infusion!=null&&result.rows.infusion==1){
    	    				$("#checkedInfusion").prop("checked",true);
    	    				$("#hospTypeDiv").parent().show();
    	    				var hLength = result.rows.hospTypeList.length;
    	    				if(hLength!=0){
    	    					$("#hospInfo").val(result.rows.hospTypeList[0].hospTypeId);
    	    					$("#hospInfoPer").val(result.rows.hospTypeList[0].expertRate);
    	    					$("#ruleHospTypeId").val(result.rows.hospTypeList[0].ruleHospTypeId);
    	    					if(hLength>1){
    	    						for(var i = 1;i<hLength;i++){
    	    							$("#hospTypeDiv").append("<div style='width:350px;' class='del'><select class='form-control hospInfo' style='width: 190px;float:left' ></select><input class='form-control hospInfoPer' style='width: 50px;float:left;margin-left:30px' oninput=\"value=value.replace(/\\D/g,'')\"/><input class='ruleHospTypeIds' type='hidden' value=''/><div style='width:30px;float:left;margin-left:10px;'>%</div><a href='javascript:void(0);' style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat -40px 0px' class='opIcon' onclick='toDelH(this);' title='删除' ></a></div>");
    	    				        	$("#hospTypeDiv").height($("#hospTypeDiv").height()+24);
    	    				        	//loadHospType($($(".hospInfo")[$(".hospInfo").length-1]));
    	    				        	$($(".hospInfo")[i-1]).HNSelect({
								            url: "${ctx}/selectController/getArchiveType.html", 
								            data: { classifyCode: "113" },
								            defaultText: "<option value=''>请选择</option>",
								            defaultselect: result.rows.hospTypeList[i].hospTypeId,
								            func:function(){
								            	 hCount = $(".hospInfo").length+1;
								            }
								        });
    	    				        	$($(".hospInfoPer")[i-1]).val(result.rows.hospTypeList[i].expertRate);
    	    				        	$($(".ruleHospTypeIds")[i-1]).val(result.rows.hospTypeList[i].ruleHospTypeId);
    	    				           
    	    						}
    	    					}
    	    				}
    	    				//////////////////////////////////
    	    			}
    	    		} 
    				var eLength = result.rows.expertTypeList.length;
    				var list = [];
    				if(eLength!=0){
    					list = result.rows.expertTypeList;
    					var a = list[0].firstLevel;
    					var b = list[0].secondLevel;
    					var c = list[0].ThiredLevel;
    					$("#dicExpertType1").HNSelect({
    				        url: "${pageContext.request.contextPath}/selectController/getExpertType.html", data: { ID: '0' },defaultselect:a,  defaultText: "<option value=''>请选择</option>",
    				        func: function () {
    				        	$("#dicExpertType2").HNSelect({
    				                parent_selector: $("#dicExpertType1"), url: "${pageContext.request.contextPath}/selectController/getExpertType.html", dataid: "ID",defaultselect:b,defaultText: "<option value=''>请选择</option>",
    				                		  func: function () {
    				                			  $("#dicExpertType3").HNSelect({
    				                                  parent_selector: $("#dicExpertType2"), url: "${pageContext.request.contextPath}/selectController/getExpertType.html", dataid: "ID", defaultselect:c,defaultText: "<option value=''>请选择</option>"
    				                              });
    				                          }
    				           		 });
    				       		 }
    				    	}); 
    					$("#dicExpertTypeNum").val(list[0].expertCount);
    					$("#bidExpertTypeId").val(list[0].bidExpertTypeId);
    					 if(eLength>1){
    						 
    						 list.splice(0,1);
    					editEt(list,eLength-1);
    					
    					} 
    				}
    			}else{
    				getExperType1("dicExpertType");
    				$.HN.message.alert('数据异常！', '信息', 'error');
    			} 
    			
    		}, "json");
    	} 
    	 
    	 function editEt(list,num){
					var d = list[0].firstLevel;
					var e = list[0].secondLevel;
					var f = list[0].ThiredLevel;
					$("#expertTypeDiv").append("<div style='width:530px;' class='del'><select class='form-control expertType1'  style='width: 120px;float:left;' ></select><select class='form-control expertType2'  style='width: 120px;float:left;' ></select><select class='form-control expertType3'  style='width: 120px;float:left;' ><option value=''>请选择</option></select><input class='form-control expertTypeNum' style='width: 50px;float:left;margin-left:30px' oninput=\"value=value.replace(/\\D/g,'')\"/><input class='bidExpertTypeIds' type='hidden'  value=''/><div style='width:30px;float:left;margin-left:10px;'>人</div><a href='javascript:void(0);' style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat -40px 0px' class='opIcon' onclick='toDelE(this);' title='删除' ></a></div>");
					$($(".expertType1")[$(".expertType1").length-1]).HNSelect({
				        url: "${pageContext.request.contextPath}/selectController/getExpertType.html", data: { ID: '0' },  defaultselect:d,defaultText: "<option value=''>请选择</option>",
				        func: function () {
				        	$($(".expertType2")[$(".expertType2").length-1]).HNSelect({
				                parent_selector: $($(".expertType1")[$(".expertType1").length-1]), url: "${pageContext.request.contextPath}/selectController/getExpertType.html", dataid: "ID",defaultselect:e,defaultText: "<option value=''>请选择</option>",
				                		  func: function () {
				                			  $($(".expertType3")[$(".expertType3").length-1]).HNSelect({
				                                  parent_selector: $($(".expertType2")[$(".expertType2").length-1]), url: "${pageContext.request.contextPath}/selectController/getExpertType.html", dataid: "ID", defaultselect:f,defaultText: "<option value=''>请选择</option>",
				                                  func:function(){
				                                	  num --;
				                                	  eCount = $(".expertType1").length+1;
				                                	  if(num>=0){
				                                		  editEt(list,num);
				                                	  }
				                                  }
				                              });
				                          }
				           		 });
				       		 }
				    	});  
					
					$($(".expertTypeNum")[$(".expertTypeNum").length-1]).val(list[0].expertCount); 
					$($(".bidExpertTypeIds")[$(".bidExpertTypeIds").length-1]).val(list[0].bidExpertTypeId); 
		        	list.splice(0,1);
    	 }
    	function clear(){
    		if($("#checkedInfusion")){
    			$("#checkedInfusion").prop("checked",false);
    		}
    		$("#hospTypeDiv").parent().hide();
    		$("#maxExtractNum").val("1");
    		$("#hospInfo").val("");
  			$('#dicExpertType1').html("");
  			$('#dicExpertType2').attr("disabled",false);
  			$('#dicExpertType3').attr("disabled",false);
  			$("#ruleId").val("");
  			$("#hospInfoPer").val("");
  			$("#dicExpertTypeNum").val("");
  			$("#ruleHospTypeId").val("");
  			$("#bidExpertTypeId").val("");
  			
  			hFlag = "";//编辑的时候看医疗机构类型变多了还是少了
  		    eFlag = "";
  		    hCount = 0;
  		    eCount = 0;
  		  	ahCount = 0; //修改后医疗机构类型个数
  	   		aeCount = 0; 
        }
    	
    	  
    	function save(){
    		
        	if (!validate()) {
    			return;
    		}   
    		var maxExtractNum  = $.trim($('#maxExtractNum').val());
    		var hospInfo = "";
    		var hospInfoPer  = "";
    		var ruleHospTypeId = "";
    		var checkedInfusion = $("#checkedInfusion");
    		var hList = [];
    		var eList = [];
    		var infusion = 0;
    		
    		if(checkedInfusion){
    			if(checkedInfusion.prop("checked")){
    				infusion = 1;
    				hospInfo = $("#hospInfo").val();
    	    		hospInfoPer  = $.trim($('#hospInfoPer').val());
    	    		ruleHospTypeId  = $.trim($('#ruleHospTypeId').val());
    	    		hList.push({"hospTypeId":hospInfo,"hospTypePer":hospInfoPer,"ruleHospTypeId":ruleHospTypeId});
    	    		if($(".hospInfo").length>0){
    	    			for(var i = 0;i<$(".hospInfo").length;i++){
    	    				hList.push({"hospTypeId":$($(".hospInfo")[i]).val(),"hospTypePer":$($(".hospInfoPer")[i]).val(),"ruleHospTypeId":$($(".ruleHospTypeIds")[i]).val()});
    	    			}
    	    		}
    			}
    		}
			var dicExpertType1 = $.trim($('#dicExpertType1').val());
			var dicExpertType2 = $.trim($('#dicExpertType2').val());
			var dicExpertType3 = $.trim($('#dicExpertType3').val());
			var expertTypeId = dicExpertType1;
			if(null != dicExpertType2&&""!=dicExpertType2){
				expertTypeId = dicExpertType2;
			}
			if(null != dicExpertType3&&""!=dicExpertType3){
				expertTypeId = dicExpertType3;
			}
			var dicExpertTypeNum = $.trim($('#dicExpertTypeNum').val());
			var bidExpertTypeId = $.trim($('#bidExpertTypeId').val());
			eList.push({"expertTypeId":expertTypeId,"expertTypeNum":dicExpertTypeNum,"bidExpertTypeId":bidExpertTypeId});
			if($(".expertType1").length>0){
				for(var i = 0;i<$(".expertType1").length;i++){
					var dicExpertType1 = $.trim($($('.expertType1')[i]).val());
					var dicExpertType2 = $.trim($($('.expertType2')[i]).val());
					var dicExpertType3 = $.trim($($('.expertType3')[i]).val());
					var expertTypeId = dicExpertType1;
					if(null != dicExpertType2&&""!=dicExpertType2){
						expertTypeId = dicExpertType2;
					}
					if(null != dicExpertType3&&""!=dicExpertType3){
						expertTypeId = dicExpertType3;
					}
					var dicExpertTypeNum = $.trim($($('.expertTypeNum')[i]).val());
					var bidExpertTypeId = $.trim($($('.bidExpertTypeIds')[i]).val());
					eList.push({"expertTypeId":expertTypeId,"expertTypeNum":dicExpertTypeNum,"bidExpertTypeId":bidExpertTypeId});
    			}
			}
			var ruleId = $.trim($('#ruleId').val());
    		$.HN.message.confirm('确定要保存吗？', '', '').on(function (e) {
    			ahCount = $(".hospInfo").length+1;
    			aeCount = $(".expertType1").length+1;
    			if(hCount){
    				if(hCount>ahCount){
    					hFlag = "delete"; 
    				}else if(hCount<ahCount){
    					hFlag = "insert";
    				}
    			}
    			
    			if(eCount){
    				if(eCount>aeCount){
    					eFlag = "delete";  
    				}else if(eCount<aeCount){
    					eFlag = "insert";
    					
    				}
    			}
    			
                	   if (e) {
       	               	$.ajax({
       	           		   type: "POST",
       	           		   url: "${ctx}/expertExtractRule/toExpertExtractEditList.html",
       	           		   data:{
       	           				projId:"${projId}",
       	           			    maxExtractNum:maxExtractNum,
       	           			    infusion:infusion,
       	           			    hList:json.jsonToString(hList),
       	           			    eList:json.jsonToString(eList),
       	           				ruleId:ruleId,
       	           			    hFlag:hFlag,
       	           			    eFlag:eFlag
       	           			    
       	           		   },
       	           		   dataType:"json",
       	           		   success: function(result){
       	           			   if(result.success){
       	           				  $.HN.message.alert(result.msg||'操作成功', '信息', 'success');
       	           				   $("#gridlist").jqGrid('setGridParam',{page:1}).trigger("reloadGrid");
       	           				   $('#exampleModal').modal('hide'); 
       	           			   }else{
       	           		    	   $.HN.message.alert(result.msg||'操作失败', '信息', 'error');
       	                			   }
       	                		   }
       	                	});
       	                } 
           });
        }
        
        function validate(){
        	var maxExtractNum = $.trim($("#maxExtractNum").val());
        	if(maxExtractNum==null||maxExtractNum==""){
        		$.alert("每家医疗机构可抽取最多专家数不能为空!","warn");
        		return false;
        	}else if(maxExtractNum.match(/\D/)){
        		$.alert("每家医疗机构可抽取最多专家数必须为正整数!","warn");
        		return false;
        	}else if(Number(maxExtractNum)>=128){
        		$.alert("每家医疗机构可抽取最多专家数过大!","warn");
        		return false;
        	}
        	if($("#checkedInfusion")){
        		if($("#checkedInfusion").prop("checked")){
        			var hosp = $("#hospTypeDiv").find("select");
        			var hospPer = $("#hospTypeDiv").find("input:visible");
        			var perCount = 0;
        			for(var i = 0;i<hosp.length;i++){
        				if($(hosp[i]).val()==""){
        					$.alert("请选择医疗机构分类!","warn");
        					return false;
        				}
        				if($(hospPer[i]).val()==""){
        					$.alert("请填写"+$(hosp[i]).children("option:selected").html()+"比例!","warn");
        					return false;
        				}else if($.trim($(hospPer[i]).val()).match(/\D/)){
        					$.alert($(hosp[i]).children("option:selected").html()+"比例必须为正整数!","warn");
        	        		return false;
        				}else if(Number($.trim($(hospPer[i]).val()))>100){
        					$.alert($(hosp[i]).children("option:selected").html()+"比例不能超过100%!","warn");
        	        		return false;
        				}
        				perCount +=Number($.trim($(hospPer[i]).val()));
        			}
        			for(var i = 0;i<hosp.length;i++){
        				for(var j = 0;j<hosp.length;j++){
        					if($(hosp[i]).val()==$(hosp[j]).val()&&i!=j){
        						$.alert("医疗机构类型不能重复!","warn");
            	        		return false;
        					}
        				}
        			}
        			if(perCount!=100){
        				$.alert("医疗机构分类总比例必须为100%!","warn");
    	        		return false;
        			}
        		}
        	}
        	
        	var eList = [];
        	var dicExpertType1 = $.trim($('#dicExpertType1').val());
			var dicExpertType2 = $.trim($('#dicExpertType2').val());
			var dicExpertType3 = $.trim($('#dicExpertType3').val());
			var expertTypeId = dicExpertType1;
			var expertType1 = $.trim($('#dicExpertType1').children("option:selected").html());
			var expertType2 = $.trim($('#dicExpertType2').children("option:selected").html());
			var expertType3 = $.trim($('#dicExpertType3').children("option:selected").html());
			var expertType = expertType1;
			if(null != dicExpertType2&&""!=dicExpertType2){
				expertTypeId = dicExpertType2;
				expertType = expertType2;
			}
			if(null != dicExpertType3&&""!=dicExpertType3){
				expertTypeId = dicExpertType3;
				expertType = expertType3;
			}
			var dicExpertTypeNum = $.trim($('#dicExpertTypeNum').val());
			eList.push({"expertTypeId":expertTypeId,"expertTypeNum":dicExpertTypeNum,"expertType":expertType});
			if($(".expertType1").length>0){
				for(var i = 0;i<$(".expertType1").length;i++){
					var dicExpertType1 = $.trim($($('.expertType1')[i]).val());
					var dicExpertType2 = $.trim($($('.expertType2')[i]).val());
					var dicExpertType3 = $.trim($($('.expertType3')[i]).val());
					var expertTypeId = dicExpertType1;
					var expertType1 = $.trim($($('.expertType1')[i]).children("option:selected").html());
					var expertType2 = $.trim($($('.expertType2')[i]).children("option:selected").html());
					var expertType3 = $.trim($($('.expertType3')[i]).children("option:selected").html());
					var expertType = expertType1;
					if(null != dicExpertType2&&""!=dicExpertType2){
						expertTypeId = dicExpertType2;
						expertType = expertType2;
					}
					if(null != dicExpertType3&&""!=dicExpertType3){
						expertTypeId = dicExpertType3;
						expertType = expertType3;
					}
					var dicExpertTypeNum = $.trim($($('.expertTypeNum')[i]).val());
					eList.push({"expertTypeId":expertTypeId,"expertTypeNum":dicExpertTypeNum,"expertType":expertType});
    			}
			}
			
			for(var i = 0;i<eList.length;i++){
				if(eList[i].expertTypeId==""){
					$.alert("请选择专家类型!","warn");
					return false;
				}
				if(eList[i].expertTypeNum==""){
					$.alert("请填写"+eList[i].expertType+"专家数量!","warn");
					return false;
				}else if(eList[i].expertTypeNum.match(/\D/)){
					$.alert(eList[i].expertType+"专家数量必须为正整数!","warn");
	        		return false;
				}else if(Number(eList[i].expertTypeNum)>127){
					$.alert(eList[i].expertType+"专家数量太大!","warn");
	        		return false;
				}
			}
			
			for(var i = 0;i<eList.length;i++){
				for(var j = 0;j<eList.length;j++){
					if(eList[i].expertTypeId==eList[j].expertTypeId&&i!=j){
						$.alert("专家类型不能重复!","warn");
    	        		return false;
					}
				}
			}
        	return true;
       }


    	function batchChangeStatus(ruleId,status) {
			var msg = "";
			if(status == 1){
				msg = "启用";
			}
			if(status == 0){
				msg = "停用";
			}
			$.HN.message.confirm("确定"+msg+"吗？", '', '').on(function (e) {
                if (e) {
		    		$.post("switchStatuss.html", {
		    			"overId" : ruleId,
		    			"status" : status
		    		}, function(result) {
		    			if (result.success) {
		    				var curpage = $("#gridlist").jqGrid('getGridParam', 'page');
		    				search(curpage);
		    				$.HN.message.alert("保存成功！", "消息", "success");
		    			} else {
		    				$.HN.message.alert("保存失败！", "消息", "error");
		    			}
		    		}, "json")
                }
             });
    	}
    	
    	function batchExtractExpert(status) {
    		var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
    		if (rowIds.length <= 0) {
    			$.HN.message.alert("请选择要操作的行！", "提示消息", "info");
    			return;
    		}
			var list = [];
			for (var i = 0; i < rowIds.length; i++) {
				list.push({"ruleId":rowIds[i]});	
			}//aaaaaaaaaaaaaaaaaaa
			$.HN.message.confirm("以下规则：<br/>抽取&nbsp;<input id='extractNum' oninput=\"value=value.replace(/\\D/g,'')\" style='width:50px;' />&nbsp;组", '规则抽取', '').on(function (e) {
                if (e) {
		    		$.post("extractExpert.html", {
		    			"ruleIds" : json.jsonToString(list),"extractNum":$.trim($("#extractNum").val())
		    		}, function(result) {
		    			if (result.success) {
		    				var curpage = $("#gridlist").jqGrid('getGridParam', 'page');
		    				search(curpage);
		    				$.HN.message.alert("抽取专家成功！", "消息", "success");
		    			} else {
		    				$.HN.message.alert(result.msg, "消息", "error");
		    			}
		    		}, "json")
                }
             });
    	}
    	
    	function search(page) {
    		var projectId = $.trim($("#projectId").val());
    		var status = $.trim($("#status").val());
    		var execStatus = $.trim($("#execStatus").val());
    		var infusion = "";
    		var execStartTime = $.trim($("#execStartTime").val());
    		var execEndTime = $.trim($("#execEndTime").val());
    		if($("#infusion")){
    			infusion = $.trim($("#infusion").val());
    		}
    		$("#gridlist").jqGrid('setGridParam',{  
    	        datatype:'json',
    	        postData:{"projectId":projectId,  "status":status, "execStatus": execStatus,"infusion":infusion,"execStartTime":execStartTime,"execEndTime":execEndTime},
    	        page : (page || 1)
    	    }).trigger("reloadGrid"); //重新载入  
    	}
    
        $(function () {
        	
        	
        	
            $("#gridlist").jqGrid({
                url: "${ctx}/expertExtractRule/getExpertExtractRuleList.html",
                mtype: "post",
                postData:{projId:"${projId}"},
                datatype: "json",
                autowidth: true,
                shrinkToFit:true,
                height: 260,
                colNames: [
                    '操作',
                    '规则编码',
                    '是否大输液',
                    '规则id',
					'所属项目id', 
					'所属项目', 
					'专家类别id', 
					'专家类别编码',
					'专家类别',
					'数量', 
					'抽取状态',
					'抽取时间',
					'抽取组数',
					'状态', 
					'更新人',
					'更新时间',
					'hospTypeList'
					
                ],
                colModel: [
                   { name: 'operate', width: 120, align:"center", sortable: false,
                           	formatter : function(val, opts, rowdata) {
       	                    	var str =  "";
       	                    	if(rowdata.execStatus==0){
       	                    		str +=  "<a href=\"javascript:edit('"+rowdata.ruleId+"');\" style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat 0px 0px' class='opIcon' title='编辑'></a>";
       	                    	}
       	               			str += "<a href='toExtractRuleLogList.html?ruleId=" + rowdata.ruleId + "&projId="+rowdata.projId+"' style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat -120px 0px' class='opIcon' title='维护记录'></a>";
       	               			if(rowdata.status==1){
       	               				str +="<a href=\"javascript:batchChangeStatus('"+rowdata.ruleId+"','0');\" style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat -40px 0px' class='opIcon' title='停用'></a>";
       	               			}else {
       	               				str +="<a href=\"javascript:batchChangeStatus('"+rowdata.ruleId+"','1');\" style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat -60px 0px' class='opIcon' title='启用'></a>";
       	               			}
       	                    	return str;
                    }},
                    { name: 'ruleCode', width: 100, align:"center", sortable: false},
                    { name: 'infusion', width: 80,align:"center", sortable: false,
                    	formatter:function(val,opts,rowdata){
                    		if(rowdata.infusion==1){
                    			return "是";
                    		}else if(rowdata.infusion==0){
                    			return "否";
                    		}else {
                    			return "";
                    		}
                    	}
                    },
                    { name: 'ruleId',hidden:true, width: 100, align:"center", sortable: false},
                    { name: 'projId',hidden:true,  width: 120, align:"center", sortable: false},
                    { name: 'name', hidden:true,width: 160, align:"center", sortable: false},
                    { name: 'expertTypeId',hidden:true, width: 100, align:"left", sortable: false},
                    { name: 'expertTypeCode',hidden:true, width: 80, align:"left", sortable: false},
                    { name: 'expertTypeName',hidden:true, width: 90, align:"center", sortable: false},
                    { name: 'count', width: 60,hidden:true, align:"center", sortable: false,
                    	formatter : function(val, opts, rowdata) {
                    		if(rowdata.exec_status==1){
                    			str = "<a href='toExtractExpertList.html?ruleId=" + rowdata.ruleId + "'>"+rowdata.count+"</a>";
                    		}else{
                    			str = rowdata.count;
                    		}
	               		   return str;
                    	}
                    },
                    
                    { name: 'execStatus', width: 60, align: 'center', sortable: false,
                    	formatter : function(val, opts, rowdata) {
                    		var strSta = "";
                    		if(rowdata.execStatus == 1){
                    			strSta += "已抽取";
                    		}else if(rowdata.execStatus == 0){
                    			strSta += "未抽取";
                    		}
                    		return strSta;
                    	}
                    },
                    { name: 'execDatetime', width: 110, align:"center", sortable: false},
                    { name: 'extractNum', width: 110, align:"center", sortable: false},
                    { name: 'status', width: 60, align: 'center', sortable: false,
                    	formatter : function(val, opts, rowdata) {
                    		var strSta = "";
                    		if(rowdata.status == 1){
                    			strSta += "启用";
                    		}else if(rowdata.status == 0){
                    			strSta += "停用";
                    		}
                    		return strSta;
                    	}
                    },
                    { name: 'updDatetime',hidden:true, width: 110, align:"center", sortable: false},
                    { name: 'updUser',hidden:true, width: 110, align:"center", sortable: false},
                    { name: 'hospTypeList', width: 60,hidden:true, align: 'center', sortable: false,
                    	formatter : function(val, opts, rowdata) {
                    		var strSta = "抽取规则列表 - ";
                    		if(rowdata.hospTypeList.length>0){
                		    		for(var i = 0;i<rowdata.hospTypeList.length;i++){
                		    			strSta+= rowdata.hospTypeList[i].dicName+":"+rowdata.hospTypeList[i].expertRate+"%   ";
                		    		}
                    		}else{
                    			strSta += "医疗机构类型等比例抽取";
                    		}
                    		return strSta;
                    	} 
                    }
                   
                ],
                rowNum: 20,
                rowList: [10, 20, 50,100],
                rownumbers: true,
                pager: "#gridpage",
                multiselect: true, 
                viewrecords: true,
                caption: "抽取规则列表",
                jsonReader: {
                    id: "ruleId"
                },
                subGrid: true,
                subGridOptions: {
            		"reloadOnExpand" : false,
            		"selectOnExpand" : false
            	},
            	subGridRowExpanded: function(subgrid_id, row_id) {
            		//恢复主表的行背景颜色
            		var rowIds = $("#gridlist").getDataIDs(); 
            		for(var i = 0;i<rowIds.length;i++){
            			$("#"+rowIds[i]).css("background-color",colorList[i].color);
            		}
            		
            		var subgrid_table_id;
            		subgrid_table_id = subgrid_id+"_t";
            		$("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table>");
            		$("#"+subgrid_table_id).jqGrid({
            			url:"getExpertTypeRuleDetailList.html",
            			postData:{"projId":"${projId}","ruleId":row_id},
            			mtype: "post",
                        datatype: "json",
                        width:900,
            			colNames: ['专家类型','每组人数'],
            			colModel: [
            				{name:"expertTypeName",index:"dicName",width:400,align:"center",sortable:false},
            				{name:"count",index:"groupNum",width:300,align:"center",sortable:false}
            			],
            		   	rowNum:0,
            		   	rownumbers: true,
            		    height: '100%',
            		    caption: "抽取规则列表",
            		    
            		    gridComplete:function(){
            		    }
            		    
            		});
            		
            		//删除没用的标签
            		$($("#"+row_id).next('.ui-subgrid').children()[0]).remove();
            		$($("#"+row_id).next('.ui-subgrid').children()[0]).remove();
            		//使子表居中
            		$("#gbox_gridlist_"+row_id+"_t").width($("#gview_gridlist").width()-26);
    		    	$("#gview_gridlist_"+row_id+"_t").css("margin","0 auto");
    		    	//改变子表题头背景颜色
    		    	$($("#gview_gridlist_"+row_id+"_t").children()[0]).css("background-color","#00a65a");
    		    	$($("#gview_gridlist_"+row_id+"_t").children()[0]).css("border-color","#008d4c");
    		    	
    		    	//修复主表展开行下一行的上边框
    		    	for(var i = 0;i<colorList.length;i++){
             			if(row_id==colorList[i].id){
             				if(colorList[i+1]){
             					$("#"+colorList[i+1].id).css("border-top","1px solid #cecece");
                 				break;
             				}
             			}
             		}
    		    	
    		    	var rowData = $("#gridlist").jqGrid('getRowData',row_id);
        	        $("#"+subgrid_table_id).setCaption(rowData.hospTypeList);
    		    	
    		    	
    		    	$(window).resize(function(){
    		    		$("#gbox_gridlist_"+row_id+"_t").width($("#gview_gridlist").width()-26);
        		    	$("#gview_gridlist_"+row_id+"_t").css("margin","0 auto");
    		    	});
            		
            	},
            	subGridRowColapsed: function(subgrid_id, row_id) { 
            	
            	},
            	loadComplete: function(){
            		colorList = [];
            		var rowIds = $("#gridlist").getDataIDs(); 
            		for(var i = 0;i<rowIds.length;i++){
            			colorList.push({"id":rowIds[i],"color":$("#"+rowIds[i]).css("background-color")});
            		}
            		$(".sgcollapsed").click();
            		
    		    }
    		    
            });
            $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
            //判断是否显示大输液列
            ${infusion==null?"//":infusion?"//":""}$("#gridlist").hideCol("infusion");
            
            $(window).trigger("resize");
            $("#projectId").val("${projId }");
            
            //设置列表题头
            $("#gridlist").setCaption($("#projectId  option:checked").html()||"抽取规则列表");
            
        });
    </script>
</body>
</html>












